package cn.jcet.module.sys.service.impl;

import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.IdUtil;
import cn.jcet.module.sys.domain.User;
import cn.jcet.module.sys.mapper.RoleMapper;
import cn.jcet.module.sys.mapper.UserMapper;
import cn.jcet.module.sys.service.UserService;
import cn.jcet.module.sys.vo.UserVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper,User> implements UserService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public boolean save(User entity) {
        return super.save(entity);
    }

    @Override
    public boolean removeById(Serializable id) {
        //根据用户角色删除角色信息
        roleMapper.deleteRoleUserByUid(id);
        return super.removeById(id);
    }

    @Override
    public boolean updateById(User entity) {
        return super.updateById(entity);
    }

    @Override
    public User getById(Serializable id) {
        return super.getById(id);
    }

    @Override
    public void saveUserRole(Integer uid, Integer[] ids) {
        this.roleMapper.deleteRoleUserByUid(uid);
        if (null != ids && ids.length>0){
            for (Integer rid:ids)
            this.roleMapper.insertUserRole(uid,rid);
        }
    }

    /**
     * 修改密码
     * @param password
     * @param newPwd
     * @param cfgPwd
     * @return
     */
    @Override
    public int updateUserPwd(String password, String newPwd, String cfgPwd) {

        if(password == null || password.length()==0){
            throw new IllegalArgumentException("请输入旧密码！");
        }
        if(newPwd==null || newPwd.length()==0) {
            throw new IllegalArgumentException("请输入新密码！");
        }
        if(cfgPwd==null || cfgPwd.length()==0) {
            throw new IllegalArgumentException("请确认新密码！");
        }
        //2.验证参数的合法性、
        //2.1判断两次输入的新密码是否一致
        if(!newPwd.equals(cfgPwd)) {
            throw new IllegalArgumentException("两次输入密码不一致！");
        }
        //2.2判断旧密码是否跟登录密码一致
        //2.2.1 先获取用户的登陆身份通过shiro认证管理工具获得
        UserVo  user = (UserVo) SecurityUtils.getSubject().getPrincipal();
        SimpleHash simpleHash = new SimpleHash("MD5", password, user.getSalt());
        //sh.toHex()是将加密后的密码转换为16进制
        if(!user.getPwd().equals(simpleHash.toHex())){
            throw new IllegalArgumentException("输入的旧密码不正确！");
        }
        //2.3判断新旧密码是否一致
        if(password.equals(newPwd)) {
            throw new IllegalArgumentException("新密码不能与旧密码相同！");
        }
        //3.更新密码
        String salt = IdUtil.simpleUUID().toUpperCase();
        simpleHash = new SimpleHash("MD5",newPwd,salt);
        int rows = this.userMapper.updateUserPwd(user.getLoginname(),simpleHash.toHex(),salt);
        return rows;
    }
}
